﻿var CurrentType={};

Ext.onReady(function () {
    PageSafe();

    //广告模板类别信息
    var modelstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Advertisement.AdvertisementModelView', 'GetAdModelType'),
        root: 'ResponseObject',
        fields: ['PID', 'TypeName', 'LastUpdate', 'NTime'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var modelsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = modelgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentType = k[0].json;
                }
            }
        }
    });
    var modelgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 550,
        sm: modelsm,
        loadMask: true, //加载
        columns: [
       modelsm,
       { id: 'TypeName', header: '广告模板类别名称', width: 160, dataIndex: 'TypeName', sortable: true },
       { id: 'NTime', header: '时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: modelstore
    });

    var mainpage = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '模板类别',
            tbar: [{ text: '新增类别', iconCls: 'add-icon', handler: function () {
                var adwin = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 130,
                    title: '广告模板类别',
                    bodyStyle: 'background:#ffffff',
                    items: new Ext.form.FormPanel({
                        iconCls: 'Advertisement-icon',
                        enctype: 'multipart/form-data',
                        method: 'POST',
                        border: false,
                        bodyStyle: 'padding-top:20px',
                        fileUpload: true,
                        id: 'ad_main',
                        defaults: { xtype: 'textfield' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '广告模板类别名称',
                            width: 160,
                            id: 'Ad_ModelTypeName',
                            name: 'Ad_ModelTypeName'
                        }]
                    }),
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("Ad_ModelTypeName").getValue() == "") { Ext.Msg.alert("检查提示", "请输入广告模板类别名称"); return; }
                        var re = new JsonRequest();
                        re.params.model = {
                            TypeName: Ext.getCmp("Ad_ModelTypeName").getValue()
                        };

                        Ext.Ajax.request({
                            url: Ext.getClassUrl('Advertisement.AdvertisementModelView', 'AddAdModelType'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", "广告模板类别增加成功");
                                    modelstore.load();
                                }
                                else {
                                    Ext.Msg.alert("操作提示", "广告模板类别增加失败，请联系管理员");
                                }
                                adwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { adwin.close(); } }]
                }).show();
            }
            }, { text: '修改类别', iconCls: 'update-icon', handler: function () {
                var upwin = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 130,
                    title: '修改广告模板类别',
                    bodyStyle: 'background:#ffffff',
                    items: new Ext.form.FormPanel({
                        iconCls: 'Advertisement-icon',
                        enctype: 'multipart/form-data',
                        method: 'POST',
                        border: false,
                        bodyStyle: 'padding-top:20px',
                        fileUpload: true,
                        id: 'ad_main',
                        defaults: { xtype: 'textfield' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '广告模板类别名称',
                            width: 160,
                            id: 'u_ModelTypeName',
                            name: 'u_ModelTypeName'
                        }]
                    }),
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("u_ModelTypeName").getValue() == "") { Ext.Msg.alert("检查提示", "请输入广告模板类别名称"); return; }
                        var re = new JsonRequest();
                        re.params.model = {
                            PID: CurrentType.PID,
                            LastUpdate: CurrentType.LastUpdate,
                            TypeName: Ext.getCmp("u_ModelTypeName").getValue()
                        };

                        Ext.Ajax.request({
                            url: Ext.getClassUrl('Advertisement.AdvertisementModelView', 'UpdateAdModelType'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", "广告模板类别修改成功");
                                    modelstore.load();
                                }
                                else {
                                    Ext.Msg.alert("操作提示", "广告模板类别修改失败，请联系管理员");
                                }
                                upwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                    listeners: {
                        render: function () {
                            Ext.getCmp("u_ModelTypeName").setValue(CurrentType.TypeName);
                        }
                    }
                }).show();
            }
            }, { text: '删除类别', iconCls: 'del-icon', handler: function () {
                if (CurrentType.PID == null) { Ext.Msg.alert("检查提示", "请选择一个广告类型"); return; }
                var re = new JsonRequest();
                re.params.model = CurrentType;

                Ext.Ajax.request({
                    url: Ext.getClassUrl('Advertisement.AdvertisementModelView', 'DelAdModelType'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("操作提示", "广告模板类别删除成功");
                            modelstore.load();
                        }
                        else {
                            Ext.Msg.alert("操作提示", "广告模板类别删除失败，请联系管理员");
                        }
                    }
                });
            }
            }],
            items: modelgrid,
            listeners: {
                render: function () {
                    modelstore.load();
                }
            }
        }]
    });
});